package com.gaofei.alipay.job;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.gaofei.alipay.api.OrderFeign;
import com.gaofei.alipay.api.TbOrder;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DoBillJob {

    @Autowired
    private OrderFeign orderFeign;

//    @XxlJob("testJobHandler")
    public void testXXLJob(){
        System.out.println("使用xxl-job执行定时任务!!!!!!!");
    }
    //处理掉单的方法
//    @Scheduled(fixedDelay = 120000)
    @XxlJob("doBillHandler")
    public void dobill() throws AlipayApiException {
        //查询所有待支付的订单
        List<TbOrder> list = orderFeign.searchOrderWithNopay("待支付");
        if(list!=null&&list.size()>0){
            for (TbOrder od : list) {
                String status = searchOrderStatusFromAlipay(od.getId()+"");
                if (status.equals("TRADE_SUCCESS")){
                    orderFeign.updateOrderStatus(od.getId());
                }
            }
        }

    }

    //去支付宝查询订单状态
    public String searchOrderStatusFromAlipay(String orderId) throws AlipayApiException {
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCspZuyimkZ0hG441eITVVQyeCfwV9BgTpDocDC7IG85gHqDFiORIsKyDKPH7O0GFAHFH4bE99o8vSfTsMFPqVmQ2Y2YgkR80wCkWkJ8GNA0CNF+CscxcasmFtIfJR5Dcoheu3zK9Ej7HzqndDNKKTV896AG+PtutO7tQhyEA16jT1fMGxy6mss8jh0N6jwHr2OtA7f8rlSM//nJmRLEJmw/ezPAkYXXH4HUS0CvrJUnnKMniM81t6129yTZI3jr3RHPhc8B5YRGV/HSebfJz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IfMYnk/Zz5Yfnq2dCQTEeAd6NoDj76om2QKBgH0VC3I9Nf1BJ8StoLsLi7lCoWfORPH519KqLt7tGv6yHb/bnwIoDKwbfkQCnnBOwC+ADuPyhgVVqGqmUuersyFdTXLp1G0+9xXQNyh6iTYaAmwUZr3XEP/EJIlXgRAg9P/hHok19nWLoUpikY8oRGY8JtOIwgQ5/TqLIA46Zsga";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAio0KicEvQ/8404EWznkT1MCvomH3iVIYnrHRVXtL3xFXfRBxRBe5GTzbDLhayz1g+a/nYShUDaSYRcpdeqr9koQTwg8946JB5wC/SFcMFQdxdu4lyR+ZiyGI9bRgevZKqYyyc4n6nb0xLghpgPIcaQOwFLHRkiFh0jQwcTX0uuF4YTvxiYTFQk1oQKU1elfemhF4bqAq/K1xPhAtNxMPxH9HokGMklyf9a3e8gPtBy1dOv7g0yB7V5HuUR+88MgCUYjaN9Hx1LtMeP0I8YAOMSxQ4xBKOG3GPzy6rkvr68gdt1y25qKaqJWDJMSpzgbjFy2tltJGUEGx+rsA0ZWjkwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000126621192");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(orderId);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        JSONObject entries = JSONUtil.parseObj(response.getBody());
        JSONObject alipay_trade_query_response = (JSONObject) entries.get("alipay_trade_query_response");
        String trade_status = (String) alipay_trade_query_response.get("trade_status");
        System.out.println(trade_status);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return trade_status;
    }
}
